﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
//using System.Runtime.Remoting;                  //for the RemotingConfiguration Class
//using System.Runtime.Remoting.Channels;         //for the ChannelServices Class
//using System.Runtime.Remoting.Channels.Tcp;     //for the TcpChannel Class
using System.Collections;
//using GameInterface;

namespace WindowsFormsApplication1
{
    public partial class Game : Form
    {
//        #region Variable

//        #region Remoting Vars

//        /// <summary>
//        /// the Server proxy.
//        /// </summary>
//        IGame ServerProxy;

//        /// <summary>
//        /// the string that will hold the IP address.
//        /// </summary>
//        private string IP;

//        #endregion

//        /// <summary>
//        /// if the ship is selected or not.
//        /// </summary>
//        static bool select = false;

//        /// <summary>
//        /// type of ship.
//        /// </summary>
//        int shipType = 0;

//        /// <summary>
//        /// size of a ship.
//        /// </summary>
//        int Size = 0;

//        /// <summary>
//        /// the direction of a ship.
//        /// </summary>
//        bool Rotation = false;

//        /// <summary>
//        /// picture of ship.
//        /// </summary>
//        private Bitmap buffer;

//        /// <summary>
//        /// if it's drawing or not.
//        /// </summary>
//        bool checking = true;

//        /// <summary>
//        /// If the last ship need to deleted or not from the List.
//        /// </summary>
//        bool Deleted = false;

//        /// <summary>
//        /// An instance of Login to be able to get back.
//        /// </summary>
//        Login log;

//        /// <summary>
//        /// List of ships.
//        /// </summary>
//        List<Ship> Ships = new List<Ship>();

//        #endregion
//        #region Methods

        public Game(string Ip, Login Log, int Size)
        {
            InitializeComponent();
//            IP = Ip;
//            log = Log;
//            setupTcp();
//            Connect();
//            panel1_Resize(this, null);
        }

//        /// <summary>
//        /// Check Ship Overlap other ship
//        /// </summary>
        private void CheckingOverlaping(int MX, int MY, int Size)
        {
//            Ships.Add(new ship(shipType, MX, MY, Size, Rotation));


//            for (int t = 0; t < Ships.Count - 1; t++)
//            {
//                if (Ships[t].rotate == true && Ships[Ships.Count - 1].rotate == true) // both are vertical
//                {
//                    for (int xcor = 0; xcor < Size; xcor++)
//                    {
//                        for (int x = 0; x < Ships[t].S; x++)
//                        {

//                            if ((Ships[t].X + (x * 30) + x == Ships[Ships.Count - 1].X + (xcor * 30) + xcor) && (Ships[t].Y == Ships[Ships.Count - 1].Y))
//                            {

//                                Deleted = true;
//                            }

//                        }
//                    }

//                }
//                if (Ships[t].rotate == false && Ships[Ships.Count - 1].rotate == false) // both are horizontal
//                {
//                    for (int xcor = 0; xcor < Size; xcor++)
//                    {
//                        for (int x = 0; x < Ships[t].S; x++)
//                        {

//                            if ((Ships[t].Y + (x * 30) + x == Ships[Ships.Count - 1].Y + (xcor * 30) + xcor) && (Ships[t].X == Ships[Ships.Count - 1].X))
//                            {

//                                Deleted = true;
//                            }

//                        }
//                    }
//                }
//                if (Ships[t].rotate == true && Ships[Ships.Count - 1].rotate == false) // ship[ship.Count-1] is horizontal
//                {
//                    for (int xcor = 0; xcor < Size; xcor++)
//                    {
//                        for (int x = 0; x < Ships[t].S; x++)
//                        {

//                            if ((Ships[t].X + (x * 30) + x == Ships[Ships.Count - 1].X) && (Ships[t].Y == Ships[Ships.Count - 1].Y + (xcor * 30) + xcor))
//                            {

//                                Deleted = true;
//                            }

//                        }
//                    }
//                }
//                if (Ships[t].rotate == false && Ships[Ships.Count - 1].rotate == true) //ship[ship.Count-1] is Vertical
//                {
//                    for (int xcor = 0; xcor < Size; xcor++)
//                    {
//                        for (int x = 0; x < Ships[t].S; x++)
//                        {

//                            if ((Ships[t].X == Ships[Ships.Count - 1].X + (x * 30) + x) && (Ships[t].Y + (xcor * 30) + xcor == Ships[Ships.Count - 1].Y))
//                            {

//                                Deleted = true;
//                            }

//                        }
//                    }

//                }

//            }
//            Ships.Remove(Ships[Ships.Count -1]);
        }

//        /// <summary>
//        /// Remove a ship from the grid
//        /// </summary>
        private void RemoveShip(int X, int Y, int width, int height, int i, bool Rotated)
        {
            
//            using (Graphics bufferGrph = Graphics.FromImage(buffer))
//            {

//                if (((X + ((i - 1) * 30) + width) < 311 && this.Rotatebtn.Text == "Horizontal") || (Y + ((i - 1) * 30) + height < 311 && this.Rotatebtn.Text == "Vertical"))
//                {

//                    for (int z = 0; z < i; z++)
//                    {
//                        if (Rotated == true)
//                        {
//                           bufferGrph.FillRectangle(new SolidBrush(Color.White), X + (z * 30) + z, Y, width, height);

//                        }
//                        else
//                        {
                            
//                            bufferGrph.FillRectangle(new SolidBrush(Color.White), X, Y + (z * 30) + z, width, height);

//                        }
//                    }
//                }
                
//            }

//            // Invalidate the panel. This will lead to a call of 'panel1_Paint'
//            panel1.Invalidate();
        }

//        private void MoveToHeadPosition(int CoorToChange)
//        {
           
//        }

//        #endregion
//        #region User Interface

        private void ShipType1_MouseClick(object sender, MouseEventArgs e)
        {
//            select = true;
//            shipType = 1;
//            Size = 2;

        }

        private void ShipType2_MouseClick(object sender, MouseEventArgs e)
        {
//            select = true;
//            shipType = 2;
//            Size = 3;
        }

        private void ShipType3_MouseClick(object sender, MouseEventArgs e)
        {
//            select = true;
//            shipType = 3;
//            Size = 3;
        }

        private void ShipType4_MouseClick(object sender, MouseEventArgs e)
        {
//            select = true;
//            shipType = 4;
//            Size = 4;
        }

        private void ShipType5_MouseClick(object sender, MouseEventArgs e)
        {
//            select = true;
//            shipType = 5;
//            Size = 5;
        }

        
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
//            e.Graphics.DrawImageUnscaled(buffer, Point.Empty);
        }

        private void panel1_Resize(object sender, EventArgs e)
        {
//            // Resize the buffer, if it is growing
//            if (buffer == null ||
//                buffer.Width < panel1.Width ||
//                buffer.Height < panel1.Height)
//            {
//                Bitmap newBuffer = new Bitmap(panel1.Width, panel1.Height);
//                if (buffer != null)
//                    using (Graphics bufferGrph = Graphics.FromImage(newBuffer))
//                        bufferGrph.DrawImageUnscaled(buffer, Point.Empty);
//                buffer = newBuffer;
//            }
        }


        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {

//            int temp;
//            temp = e.X % 31;
//            int MX = (e.X + 1) - temp;
//            temp = e.Y % 31;
//            int MY = (e.Y + 1) - temp;


//            if (Ships.Count > 1 && select == true)
//            {
//                CheckingOverlaping(MX, MY, Size);

//            }

//            if (select == true && Deleted == false)
//            {
//                switch (shipType)
//                {
//                    case 1:
//                        PaintBlueRectangle(MX, MY, 30, 30, Size);

//                        if (checking == true)
//                        {
//                            ShipType1.Visible = false;

//                            Ships.Add(new ship(shipType, MX, MY, Size, Rotation));

//                        }
//                        break;

//                    case 2:
//                        PaintBlueRectangle(MX, MY, 30, 30, Size);

//                        if (checking == true)
//                        {
//                            ShipType2.Visible = false;
//                            Ships.Add(new ship(shipType, MX, MY, Size, Rotation));
//                        }
//                        checking = true;
//                        break;
//                    case 3:
//                        PaintBlueRectangle(MX, MY, 30, 30, Size);

//                        if (checking == true)
//                        {
//                            ShipType3.Visible = false;
//                            Ships.Add(new ship(shipType, MX, MY, Size, Rotation));
//                        }
//                        checking = true;
//                        break;
//                    case 4:
//                        PaintBlueRectangle(MX, MY, 30, 30, Size);

//                        if (checking == true)
//                        {
//                            ShipType4.Visible = false;
//                            Ships.Add(new ship(shipType, MX, MY, Size, Rotation));
//                        }
//                        checking = true;
//                        break;
//                    case 5:
//                        PaintBlueRectangle(MX, MY, 30, 30, Size);

//                        if (checking == true)
//                        {
//                            ShipType5.Visible = false;
//                            Ships.Add(new ship(shipType, MX, MY, Size, Rotation));
//                        }
//                        checking = true;
//                        break;

//                }
//                select = false;
//            }
//            else if (select == false)
//            {
//                int Sizeresult = 1;
//                int Type = 0;
//                bool R = false;
//                for (int search = 0; search < Ships.Count; search++)
//                {
//                    if (MX == Ships[search].X && MY == Ships[search].Y)
//                    {
//                        Type = Ships[search].Type;
//                        R = Ships[search].rotate;
//                        Ships.Remove(Ships[search]);
//                    }
//                    else if (MY == Ships[search].Y && Ships[search].X <= MX && MX <= Ships[search].X + (Ships[search].S * 30))
//                    {
//                        MX = Ships[search].X;
//                        MY = Ships[search].Y;
//                        Type = Ships[search].Type;
//                        R = Ships[search].rotate;
//                        Ships.Remove(Ships[search]);
//                    }
//                    else if (MX == Ships[search].X && Ships[search].Y <= MY && MY <= Ships[search].Y + (Ships[search].S * 30))
//                    {
//                        MX = Ships[search].X;
//                        MY = Ships[search].Y;
//                        Type = Ships[search].Type;
//                        R = Ships[search].rotate;
//                        Ships.Remove(Ships[search]);
//                    }

//                }
//                switch (Type)
//                {
//                    case 1:
//                        Sizeresult = 2;
//                        if (ShipType1.Visible == false)
//                        {
//                            ShipType1.Visible = true;
//                        }
//                        break;
//                    case 2:
//                        Sizeresult = 3;
//                        if (ShipType2.Visible == false)
//                        {
//                            ShipType2.Visible = true;
//                        }
//                        break;
//                    case 3:
//                        Sizeresult = 3;
//                        if (ShipType3.Visible == false)
//                        {
//                            ShipType3.Visible = true;
//                        }
//                        break;
//                    case 4:
//                        Sizeresult = 4;
//                        if (ShipType4.Visible == false)
//                        {
//                            ShipType4.Visible = true;
//                        }
//                        break;
//                    case 5:
//                        Sizeresult = 5;
//                        if (ShipType5.Visible == false)
//                        {
//                            ShipType5.Visible = true;
//                        }
//                        break;
//                }

//                RemoveShip(MX, MY, 30, 30, Sizeresult, R);

//            }
//            else if (select == true && Deleted == true)
//            {
//                //Ships.Remove(Ships[Ships.Count - 1]);
//                MessageBox.Show("Overlap ships");
//                Deleted = false;
//            }

        }

        private void Rotatebtn_Click(object sender, EventArgs e)
        {
//            if (this.Rotatebtn.Text == "Vertical")
//            {
//                this.Rotatebtn.Text = "Horizontal";
//                Rotation = false;
//            }
//            else
//            {
//                this.Rotatebtn.Text = "Vertical";
//                Rotation = true;
//            }
        }

        private void PaintBlueRectangle(int X, int Y, int width, int height, int i)
        {
//            // Draw blue rectangle into the buffer

//            using (Graphics bufferGrph = Graphics.FromImage(buffer))
//            {

//                if (((X + ((i - 1) * 30) + width) < 311 && this.Rotatebtn.Text == "Vertical") || (Y + ((i - 1) * 30) + height < 311 && this.Rotatebtn.Text == "Horizontal"))
//                {
//                  for (int z = 0; z < i; z++)
//                    {
                      
//                            if (this.Rotatebtn.Text == "Vertical")
//                            {
//                                bufferGrph.FillRectangle(new SolidBrush(Color.Blue), X + (z * 30) + z, Y, width, height);
                                
//                            }
//                            else
//                            {
//                                bufferGrph.FillRectangle(new SolidBrush(Color.Blue), X, Y + (z * 30) + z, width, height);
//                            }
                    
//                    }
//                }
//                else
//                {
//                    checking = false;
//                    MessageBox.Show("out of the grid");
//                }
//            }

//            // Invalidate the panel. This will lead to a call of 'panel1_Paint'
//            panel1.Invalidate();
        }



//        #endregion
    }
}
